package com.ifcar.app.fragment;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.ifcar.app.view.LoadingListView;
import com.ifcar.app.activity.R;

public abstract class NRAbsLoadingListFrament extends NRLoadingFrament {
	private LoadingListView mLoadingListView;

	public abstract ListAdapter getAdapter();

	private OnClickListener mMoreClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			startLoadingMore();
		}
	};
	
	private OnClickListener mRefreshClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            refresh();
        }
    };

	public void attachLoadingListView(LoadingListView loadingListView) {
		if (loadingListView == mLoadingListView) {
			return;
		}
		loadingListView.reset();
		mLoadingListView = loadingListView;
		
		Button moreButton = getMoreButton();
		if (moreButton != null) {
			moreButton.setOnClickListener(mMoreClickListener);
			if (inLoadingMore()) {
			    moreButton.setText(getLoadingText());
			} else {
			    moreButton.setText(getLoadingMoreText());
			}
		}
		
		View emptyView = loadingListView.getEmptyView();
		if (emptyView != null) {
		    emptyView.setOnClickListener(mRefreshClickListener);
		}

		onLoadingListViewAttached(loadingListView);
	}

	public void detachLoadingListView() {
		if (mLoadingListView != null) {
			Button moreButton = getMoreButton();
			if (moreButton != null) {
				moreButton.setOnClickListener(null);
			}
			
			View emptyView = mLoadingListView.getEmptyView();
	        if (emptyView != null) {
	            emptyView.setOnClickListener(mRefreshClickListener);
	        }
			
			onLoadingListViewDetached(mLoadingListView);
			mLoadingListView.reset();
			mLoadingListView = null;
		}
	}

	/**
	 * 当一个LoadingListView设置时会调用
	 */
    public void onLoadingListViewAttached(LoadingListView loadingListView) {        
        if (isMoreViewShow()) {
            callLoadingListViewAddMoreView();
        }
        
        ListAdapter adapter = getAdapter();
        setAdatper(loadingListView, adapter);
        
        if (inLoading()) {
            callLoadingListViewStartLoading(adapter == null);
        } else if (inRefreshing()) {
            callLoadingListViewStartLoading(adapter == null);
        } else if (inLoadingMore()) {
            callLoadingListViewStartLoadingMore(getLoadingStart() + getLoadingNum());
        }
    }
    
    void setAdatper(LoadingListView loadingListView, ListAdapter adapter) {
        loadingListView.getListView().setAdapter(adapter);
    }

	/**
	 * 当清空LoadingListView时会调用
	 */
	public void onLoadingListViewDetached(LoadingListView loadingListView) {
	}
	
    /**
     * listview是否显示more view
     * 
     * @return
     */
    public boolean isMoreViewShow() {
        return hasMoreView() && getAdapter() != null && !getAdapter().isEmpty();
    }

	/**
	 * listview是否包含more view
	 * @return
	 */
	public boolean hasMoreView() {
		return true;
	}

	/**
	 * 设置LoadingList状态
	 * 
	 * @param isFirst
	 */
	public final void callLoadingListViewStartLoading(boolean isFirst) {
		if (mLoadingListView != null) {
			mLoadingListView.startLoading(isFirst);
		}
	}

	/**
	 * 设置LoadingList状态 同时会更新loadStart
	 * 
	 * @param isFirst
	 */
	public final void callLoadingListViewCompleteLoading(boolean success,
			boolean hasMore) {
		if (mLoadingListView != null) {
			mLoadingListView.completeLoading(success, hasMore);
		}
		if (success) {
			setLoadingStart(0);
		}
	}

	/**
	 * 设置LoadingList状态
	 * 
	 * @param isFirst
	 */
	public final void callLoadingListViewStartLoadingMore(int start) {
		if (mLoadingListView != null) {
			mLoadingListView.startLoadingMore(start);
		}
	}

	/**
	 * 设置LoadingList状态 同时会更新loadStart
	 * 
	 * @param isFirst
	 */
	public final void callLoadingListViewCompleteLoadingMore(boolean success,
			boolean hasMore) {
		if (mLoadingListView != null) {
			mLoadingListView.completeLoadingMore(success, hasMore);
		}
		if (success) {
			setLoadingStart(getLoadingStart() + getLoadingNum());
		}
	}

	/**
	 * 添加更多按钮
	 * 
	 * @param isFirst
	 */
	public final void callLoadingListViewAddMoreView() {
		if (hasMoreView() && mLoadingListView != null) {
			mLoadingListView.addMoreView();
		}
	}
	
	/**
     * 删除更多按钮
     * 
     * @param isFirst
     */
    public final void callLoadingListViewRemoveMoreView() {
        if (hasMoreView() && mLoadingListView != null) {
            mLoadingListView.removeMoreView();
        }
    }

	public final LoadingListView getLoadingListView() {
		return mLoadingListView;
	}

	public final ListView getListView() {
		return mLoadingListView != null ? mLoadingListView.getListView() : null;
	}

	public final Button getMoreButton() {
		if (!hasMoreView() || mLoadingListView == null) {
			return null;
		}
		View moreButton = mLoadingListView.getLoadingMoreClickView();
		if (moreButton != null && moreButton instanceof Button) {
			return (Button) moreButton;
		}
		return null;
	}

	public void goToTop() {
		ListView listView = getListView();
		if (listView != null) {
			listView.setSelection(0);
		}
	}

	@Override
	public boolean onActivityViewClick(View view) {
		if (view.getId() == R.id.top_main_bottom) {
			goToTop();
		}
		return super.onActivityViewClick(view);
	}

	public String getLoadingMoreText() {
		return getResources().getString(R.string.get_more,
				getLoadingNum());
	}

	public String getLoadingText() {
		return getResources().getString(R.string.loading);
	}

	@Override
	public void setLoadingNum(int num) {
		super.setLoadingNum(num);
		Button moreButton = getMoreButton();
		if (moreButton != null) {
			moreButton.setText(getLoadingMoreText());
		}

	}

	@Override
	protected void onLoadingMore() {
		super.onLoadingMore();
		Button moreButton = getMoreButton();
		if (moreButton != null) {
			moreButton.setText(getLoadingText());
		}
	}

	@Override
	protected void onHandleLoadingMore(Object result) {
		super.onHandleLoadingMore(result);

		Button moreButton = getMoreButton();
		if (moreButton != null) {
			moreButton.setText(getLoadingMoreText());
		}
	}
}
